package com.by.service;

import com.by.constant.MessageConstant;
import com.by.entity.Result;
import com.by.mapper.MemberMapper;
import com.by.mapper.OrderMapper;
import com.by.mapper.OrderSettingMapper;
import com.by.mapper.ValidateCodeMapper;
import com.by.pojo.Member;
import com.by.pojo.Order;
import com.by.pojo.OrderSetting;
import com.by.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.TypeInfo;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ValidateCodeMapper validateCodeMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result submit(Map map) throws Exception {

        //1、校验页面输入的验证码是否输入正确
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        String code = validateCodeMapper.getValidateCode(telephone, validateCode);

        if (code == null || "".equals(code)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        int second = validateCodeMapper.isValiddateCodeTimeout(telephone, validateCode);
        if (second >= 60) {
            return new Result(false, MessageConstant.VALIDATECODE_TIMEOUT);
        }

        //2、查询用户所选择的日期是否已经提前进行了预约设置
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingMapper.getOrderSettingByOrderDate(orderDate);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //3、判断当前预约日期的预约人数是否约满
        if (orderSetting.getNumber() == orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //5、检查当前用户是否为会员，如果不是会员则自动完成注册
        Member member = memberMapper.getMemberByTelephone(telephone);
        if (member == null) {
            String name = (String) map.get("name");
            String idCard = (String) map.get("idCard");
            String sex = (String) map.get("sex");
            member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberMapper.addMember(member);//主键回填
        }

        //4、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐，如果是重复预约则无法完成再次预约，不重复则直接预约)
        Integer setmealId = Integer.valueOf((String) map.get("setmealId"));
        Order order = orderMapper.getOrder(member.getId(), orderDate, setmealId);

        if(order != null){
            //已经预约过啦
            return new Result(false, MessageConstant.HAS_ORDERED);
        }

        order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);
        //数据回显
        orderMapper.addOrder(order);

        //6、预约成功，更新当日的已预约人数
        orderSettingMapper.updateOrderSettingByOrderDate(orderDate);
        return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) {
        return orderMapper.findById(id);
    }
}
